/*
 * Copyright 2009 Armando Blancas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package samples.tasks.text;

import taskgraph.channels.CharChannel;
import taskgraph.tasks.Graph;
import taskgraph.tasks.io.ReadTextFile;
import taskgraph.tasks.io.StandardOutput;

/**
 * Ready-made graph for counting words from a file
 * and writing the counts to the standard output.
 * Graphs with specific functionality can be reused
 * with little effort.
 *  
 * Unlike a composite tasks, graphs have no input or 
 * output ports and may be executed right after created.
 * Composite tasks usually require ports arguments
 * and will be added to a graph to run.
 * 
 * @author Armando Blancas
 */
public class RhymesGraph extends Graph {

	private String filename;
	
	public RhymesGraph(String filename) {
		this.filename = filename;
	}
	
	/**
	 * @return the filename
	 */
	public String getFilename() {
		return filename;
	}

	/**
	 * @param filename the filename to set
	 */
	public void setFilename(String filename) {
		this.filename = filename;
	}

	/**
	 * This is a rhymes-finding graph ready-to-go.
     * <p><pre>
     * [ReadText] --ch1--> [FindRhymes] --ch2--> [Stdout]
     * </pre>
	 */
	@Override
	protected void setup() {
		assert filename != null;
		
		CharChannel ch1 = new CharChannel();
		CharChannel ch2 = new CharChannel();
		
		add(new ReadTextFile(ch1.getOutputPort(), filename));
        add(new FindRhymes(ch1.getInputPort(), ch2.getOutputPort()));
        add(new StandardOutput(ch2.getInputPort())); 
	}
	
}
